Passed
Pull Request — master (#45)
by
unknown
06:19
created

collapse.js ➔ _objectSpread   B

Complexity

Conditions 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
dl 18
loc 18
rs 8.6666
1
/*!
2
  * Bootstrap collapse.js v4.5.3 (https://getbootstrap.com/)
3
  * Copyright 2011-2020 The Bootstrap Authors (https://github.com/twbs/bootstrap/graphs/contributors)
4
  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
  */
6
(function (global, factory) {
7
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('jquery'), require('./util.js')) :
8
  typeof define === 'function' && define.amd ? define(['jquery', './util.js'], factory) :
0 ignored issues
show
Bug introduced by
The variable define seems to be never declared. If this is a global, consider adding a /** global: define */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
9
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Collapse = factory(global.jQuery, global.Util));
0 ignored issues
show
Best Practice introduced by
If you intend to check if the variable self is declared in the current environment, consider using typeof self === "undefined" instead. This is safe if the variable is not actually declared.
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
Bug introduced by
The variable globalThis seems to be never declared. If this is a global, consider adding a /** global: globalThis */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
10
}(this, (function ($, Util) { 'use strict';
11
12
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
14
  var $__default = /*#__PURE__*/_interopDefaultLegacy($);
15
  var Util__default = /*#__PURE__*/_interopDefaultLegacy(Util);
16
17
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
0 ignored issues
show
Comprehensibility introduced by
It seems like you are trying to overwrite a function name here. _extends is already defined in line 17 as a function. While this will work, it can be very confusing.
Loading history...
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
18
19
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
20
21
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
  /**
23
   * ------------------------------------------------------------------------
24
   * Constants
25
   * ------------------------------------------------------------------------
26
   */
27
28
  var NAME = 'collapse';
29
  var VERSION = '4.5.3';
30
  var DATA_KEY = 'bs.collapse';
31
  var EVENT_KEY = "." + DATA_KEY;
32
  var DATA_API_KEY = '.data-api';
33
  var JQUERY_NO_CONFLICT = $__default['default'].fn[NAME];
34
  var Default = {
35
    toggle: true,
36
    parent: ''
37
  };
38
  var DefaultType = {
39
    toggle: 'boolean',
40
    parent: '(string|element)'
41
  };
42
  var EVENT_SHOW = "show" + EVENT_KEY;
43
  var EVENT_SHOWN = "shown" + EVENT_KEY;
44
  var EVENT_HIDE = "hide" + EVENT_KEY;
45
  var EVENT_HIDDEN = "hidden" + EVENT_KEY;
46
  var EVENT_CLICK_DATA_API = "click" + EVENT_KEY + DATA_API_KEY;
47
  var CLASS_NAME_SHOW = 'show';
48
  var CLASS_NAME_COLLAPSE = 'collapse';
49
  var CLASS_NAME_COLLAPSING = 'collapsing';
50
  var CLASS_NAME_COLLAPSED = 'collapsed';
51
  var DIMENSION_WIDTH = 'width';
52
  var DIMENSION_HEIGHT = 'height';
53
  var SELECTOR_ACTIVES = '.show, .collapsing';
54
  var SELECTOR_DATA_TOGGLE = '[data-toggle="collapse"]';
55
  /**
56
   * ------------------------------------------------------------------------
57
   * Class Definition
58
   * ------------------------------------------------------------------------
59
   */
60
61
  var Collapse = /*#__PURE__*/function () {
62
    function Collapse(element, config) {
63
      this._isTransitioning = false;
64
      this._element = element;
65
      this._config = this._getConfig(config);
66
      this._triggerArray = [].slice.call(document.querySelectorAll("[data-toggle=\"collapse\"][href=\"#" + element.id + "\"]," + ("[data-toggle=\"collapse\"][data-target=\"#" + element.id + "\"]")));
67
      var toggleList = [].slice.call(document.querySelectorAll(SELECTOR_DATA_TOGGLE));
68
69
      for (var i = 0, len = toggleList.length; i < len; i++) {
70
        var elem = toggleList[i];
71
        var selector = Util__default['default'].getSelectorFromElement(elem);
72
        var filterElement = [].slice.call(document.querySelectorAll(selector)).filter(function (foundElem) {
73
          return foundElem === element;
74
        });
75
76
        if (selector !== null && filterElement.length > 0) {
77
          this._selector = selector;
78
79
          this._triggerArray.push(elem);
80
        }
81
      }
82
83
      this._parent = this._config.parent ? this._getParent() : null;
84
85
      if (!this._config.parent) {
86
        this._addAriaAndCollapsedClass(this._element, this._triggerArray);
87
      }
88
89
      if (this._config.toggle) {
90
        this.toggle();
91
      }
92
    } // Getters
93
94
95
    var _proto = Collapse.prototype;
96
97
    // Public
98
    _proto.toggle = function toggle() {
99
      if ($__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
100
        this.hide();
101
      } else {
102
        this.show();
103
      }
104
    };
105
106
    _proto.show = function show() {
107
      var _this = this;
108
109
      if (this._isTransitioning || $__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
110
        return;
111
      }
112
113
      var actives;
114
      var activesData;
115
116
      if (this._parent) {
117
        actives = [].slice.call(this._parent.querySelectorAll(SELECTOR_ACTIVES)).filter(function (elem) {
118
          if (typeof _this._config.parent === 'string') {
119
            return elem.getAttribute('data-parent') === _this._config.parent;
120
          }
121
122
          return elem.classList.contains(CLASS_NAME_COLLAPSE);
123
        });
124
125
        if (actives.length === 0) {
126
          actives = null;
127
        }
128
      }
129
130
      if (actives) {
131
        activesData = $__default['default'](actives).not(this._selector).data(DATA_KEY);
132
133
        if (activesData && activesData._isTransitioning) {
134
          return;
135
        }
136
      }
137
138
      var startEvent = $__default['default'].Event(EVENT_SHOW);
139
      $__default['default'](this._element).trigger(startEvent);
140
141
      if (startEvent.isDefaultPrevented()) {
142
        return;
143
      }
144
145
      if (actives) {
146
        Collapse._jQueryInterface.call($__default['default'](actives).not(this._selector), 'hide');
147
148
        if (!activesData) {
149
          $__default['default'](actives).data(DATA_KEY, null);
150
        }
151
      }
152
153
      var dimension = this._getDimension();
154
155
      $__default['default'](this._element).removeClass(CLASS_NAME_COLLAPSE).addClass(CLASS_NAME_COLLAPSING);
156
      this._element.style[dimension] = 0;
157
158
      if (this._triggerArray.length) {
159
        $__default['default'](this._triggerArray).removeClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', true);
160
      }
161
162
      this.setTransitioning(true);
163
164
      var complete = function complete() {
165
        $__default['default'](_this._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
166
        _this._element.style[dimension] = '';
167
168
        _this.setTransitioning(false);
169
170
        $__default['default'](_this._element).trigger(EVENT_SHOWN);
171
      };
172
173
      var capitalizedDimension = dimension[0].toUpperCase() + dimension.slice(1);
174
      var scrollSize = "scroll" + capitalizedDimension;
175
      var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
176
      $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
177
      this._element.style[dimension] = this._element[scrollSize] + "px";
178
    };
179
180
    _proto.hide = function hide() {
181
      var _this2 = this;
182
183
      if (this._isTransitioning || !$__default['default'](this._element).hasClass(CLASS_NAME_SHOW)) {
184
        return;
185
      }
186
187
      var startEvent = $__default['default'].Event(EVENT_HIDE);
188
      $__default['default'](this._element).trigger(startEvent);
189
190
      if (startEvent.isDefaultPrevented()) {
191
        return;
192
      }
193
194
      var dimension = this._getDimension();
195
196
      this._element.style[dimension] = this._element.getBoundingClientRect()[dimension] + "px";
197
      Util__default['default'].reflow(this._element);
198
      $__default['default'](this._element).addClass(CLASS_NAME_COLLAPSING).removeClass(CLASS_NAME_COLLAPSE + " " + CLASS_NAME_SHOW);
199
      var triggerArrayLength = this._triggerArray.length;
200
201
      if (triggerArrayLength > 0) {
202
        for (var i = 0; i < triggerArrayLength; i++) {
203
          var trigger = this._triggerArray[i];
204
          var selector = Util__default['default'].getSelectorFromElement(trigger);
205
206
          if (selector !== null) {
207
            var $elem = $__default['default']([].slice.call(document.querySelectorAll(selector)));
208
209
            if (!$elem.hasClass(CLASS_NAME_SHOW)) {
210
              $__default['default'](trigger).addClass(CLASS_NAME_COLLAPSED).attr('aria-expanded', false);
211
            }
212
          }
213
        }
214
      }
215
216
      this.setTransitioning(true);
217
218
      var complete = function complete() {
219
        _this2.setTransitioning(false);
220
221
        $__default['default'](_this2._element).removeClass(CLASS_NAME_COLLAPSING).addClass(CLASS_NAME_COLLAPSE).trigger(EVENT_HIDDEN);
222
      };
223
224
      this._element.style[dimension] = '';
225
      var transitionDuration = Util__default['default'].getTransitionDurationFromElement(this._element);
226
      $__default['default'](this._element).one(Util__default['default'].TRANSITION_END, complete).emulateTransitionEnd(transitionDuration);
227
    };
228
229
    _proto.setTransitioning = function setTransitioning(isTransitioning) {
230
      this._isTransitioning = isTransitioning;
231
    };
232
233
    _proto.dispose = function dispose() {
234
      $__default['default'].removeData(this._element, DATA_KEY);
235
      this._config = null;
236
      this._parent = null;
237
      this._element = null;
238
      this._triggerArray = null;
239
      this._isTransitioning = null;
240
    } // Private
241
    ;
242
243
    _proto._getConfig = function _getConfig(config) {
244
      config = _extends({}, Default, config);
245
      config.toggle = Boolean(config.toggle); // Coerce string values
246
247
      Util__default['default'].typeCheckConfig(NAME, config, DefaultType);
248
      return config;
249
    };
250
251
    _proto._getDimension = function _getDimension() {
252
      var hasWidth = $__default['default'](this._element).hasClass(DIMENSION_WIDTH);
253
      return hasWidth ? DIMENSION_WIDTH : DIMENSION_HEIGHT;
254
    };
255
256
    _proto._getParent = function _getParent() {
257
      var _this3 = this;
258
259
      var parent;
260
261
      if (Util__default['default'].isElement(this._config.parent)) {
262
        parent = this._config.parent; // It's a jQuery object
263
264
        if (typeof this._config.parent.jquery !== 'undefined') {
265
          parent = this._config.parent[0];
266
        }
267
      } else {
268
        parent = document.querySelector(this._config.parent);
269
      }
270
271
      var selector = "[data-toggle=\"collapse\"][data-parent=\"" + this._config.parent + "\"]";
272
      var children = [].slice.call(parent.querySelectorAll(selector));
273
      $__default['default'](children).each(function (i, element) {
274
        _this3._addAriaAndCollapsedClass(Collapse._getTargetFromElement(element), [element]);
275
      });
276
      return parent;
277
    };
278
279
    _proto._addAriaAndCollapsedClass = function _addAriaAndCollapsedClass(element, triggerArray) {
280
      var isOpen = $__default['default'](element).hasClass(CLASS_NAME_SHOW);
281
282
      if (triggerArray.length) {
283
        $__default['default'](triggerArray).toggleClass(CLASS_NAME_COLLAPSED, !isOpen).attr('aria-expanded', isOpen);
284
      }
285
    } // Static
286
    ;
287
288
    Collapse._getTargetFromElement = function _getTargetFromElement(element) {
289
      var selector = Util__default['default'].getSelectorFromElement(element);
290
      return selector ? document.querySelector(selector) : null;
291
    };
292
293
    Collapse._jQueryInterface = function _jQueryInterface(config) {
294
      return this.each(function () {
295
        var $element = $__default['default'](this);
296
        var data = $element.data(DATA_KEY);
297
298
        var _config = _extends({}, Default, $element.data(), typeof config === 'object' && config ? config : {});
299
300
        if (!data && _config.toggle && typeof config === 'string' && /show|hide/.test(config)) {
301
          _config.toggle = false;
302
        }
303
304
        if (!data) {
305
          data = new Collapse(this, _config);
306
          $element.data(DATA_KEY, data);
307
        }
308
309
        if (typeof config === 'string') {
310
          if (typeof data[config] === 'undefined') {
311
            throw new TypeError("No method named \"" + config + "\"");
312
          }
313
314
          data[config]();
315
        }
316
      });
317
    };
318
319
    _createClass(Collapse, null, [{
320
      key: "VERSION",
321
      get: function get() {
322
        return VERSION;
323
      }
324
    }, {
325
      key: "Default",
326
      get: function get() {
327
        return Default;
328
      }
329
    }]);
330
331
    return Collapse;
332
  }();
333
  /**
334
   * ------------------------------------------------------------------------
335
   * Data Api implementation
336
   * ------------------------------------------------------------------------
337
   */
338
339
340
  $__default['default'](document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
341
    // preventDefault only for <a> elements (which change the URL) not inside the collapsible element
342
    if (event.currentTarget.tagName === 'A') {
343
      event.preventDefault();
344
    }
345
346
    var $trigger = $__default['default'](this);
347
    var selector = Util__default['default'].getSelectorFromElement(this);
348
    var selectors = [].slice.call(document.querySelectorAll(selector));
349
    $__default['default'](selectors).each(function () {
350
      var $target = $__default['default'](this);
351
      var data = $target.data(DATA_KEY);
352
      var config = data ? 'toggle' : $trigger.data();
353
354
      Collapse._jQueryInterface.call($target, config);
355
    });
356
  });
357
  /**
358
   * ------------------------------------------------------------------------
359
   * jQuery
360
   * ------------------------------------------------------------------------
361
   */
362
363
  $__default['default'].fn[NAME] = Collapse._jQueryInterface;
364
  $__default['default'].fn[NAME].Constructor = Collapse;
365
366
  $__default['default'].fn[NAME].noConflict = function () {
367
    $__default['default'].fn[NAME] = JQUERY_NO_CONFLICT;
368
    return Collapse._jQueryInterface;
369
  };
370
371
  return Collapse;
372
373
})));
374
//# sourceMappingURL=collapse.js.map
375